<?php

class AdseController extends BaseController
{
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * 添加广告
	 * 
	 */
	public function actionCreate()
	{
		$model=new Adse;
		if(isset($_POST['Adse']))
		{
			$model->attributes = $_POST['Adse'];
			//关联分类数据，逗号隔开
			$model->zds_category = isset($_POST['Adse']['categorys'])&&count($_POST['Adse']['categorys']) ? implode(',', $_POST['Adse']['categorys']) : '';
			//时间转为Unix 时间戳
			$model->zds_start_time = isset($_POST['Adse']['zds_start_time'])&&$_POST['Adse']['zds_start_time'] ? strtotime($_POST['Adse']['zds_start_time']) : '';
			$model->zds_end_time = isset($_POST['Adse']['zds_end_time'])&&$_POST['Adse']['zds_end_time'] ? strtotime($_POST['Adse']['zds_end_time']) : '';
			if($model->save()){
				$this->render('../index/success',array(
						'message'=>'广告添加成功！',
						'links'=>array(
								array('继续添加',$this->pre_module_url.'/adse/create/zdzid/'.$_POST['Adse']['zdz_id']),
								array('广告管理',$this->pre_module_url.'/adse/admin/zdz_id/'.$_POST['Adse']['zdz_id']),

						),
				));
				exit;
			}else{
				$this->render('../index/error',array(
						'message'=>'广告添加失败！',						
				));
				exit;
			}
		}
		$model->zdz_id = Yii::app()->request->getParam('zdzid');
		$adzone = Adzone::model()->findByPk($model->zdz_id);
		//菜单数据
		$menu1 = $menu2 = $menu3 = $key = '';
		$menu1 = Nav::model()->findByPk($adzone->zna_id);
		//分类数据
		$category = array();
		if($adzone->zdz_associate==1){
			$category = Category::model()->GetCatCache();
			$key = array('id'=>'zca_id','title'=>'zca_title');
		}elseif($adzone->zdz_associate==2){
			$key = array('id'=>'znc_id','title'=>'znc_title');
			$category = NewsCat::model()->getNewsCat();
			//print_r($category);exit;
		}
		//若存在父菜单
		if($menu1 && $menu1->zna_parentid!=0){
			$menu2 = Nav::model()->findByPk($menu1->zna_parentid);
			//保证$menu1是一级菜单,以此类推
			list($menu1,$menu2) = array($menu2,$menu1);
			if($menu1 && $menu1->zna_parentid!=0){
				$menu3 = Nav::model()->findByPk($menu1->zna_parentid);
				list($menu1,$menu2,$menu3) = array($menu3,$menu1,$menu2);
			}
		}
		$this->render('create',array(
				'model'=>$model,
				'menu1'=>$menu1,
				'menu2'=>$menu2,
				'menu3'=>$menu3,
				'adzone'=>$adzone,
				'category'=>$category,
				'catkey'=>$key,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate(){
		if(isset($_POST['Adse'])){
			$model = Adse::model()->findByPk($_POST['Adse']['zds_id']);
			$model->attributes = $_POST['Adse'];
			//关联分类数据，逗号隔开
			$model->zds_category = isset($_POST['Adse']['categorys'])&&count($_POST['Adse']['categorys']) ? implode(',', $_POST['Adse']['categorys']) : '';
			//时间转为Unix 时间戳
			$model->zds_start_time = isset($_POST['Adse']['zds_start_time'])&&$_POST['Adse']['zds_start_time'] ? strtotime($_POST['Adse']['zds_start_time']) : '';
			$model->zds_end_time = isset($_POST['Adse']['zds_end_time'])&&$_POST['Adse']['zds_end_time'] ? strtotime($_POST['Adse']['zds_end_time']) : '';
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}else{
				$this->render('../index/error',array(
						'message'=>'广告添加失败！',
						'links'=>array(
								array('广告管理',$this->pre_module_url.'/adse/admin'),
						),
				));
				exit;
			}
		}
		//载入要修改的模型并处理数据显示方式
		$model = $this->loadModel(Yii::app()->request->getParam('id'));
		$model->zds_start_time = date('Y-m-d',$model->zds_start_time);
		$model->zds_end_time = date('Y-m-d',$model->zds_end_time);
		$model->zds_category = explode(',',$model->zds_category);
		//广告位数据
		$adzone = Adzone::model()->findByPk($model->zdz_id);
		//菜单数据
		$menu1 = $menu2 = $menu3 = $key = '';
		$menu1 = Nav::model()->findByPk($adzone->zna_id);
		//关联分类数据
		$category = array();
		if($adzone->zdz_associate==1){
			$category = Category::model()->GetCatCache();
			$key = array('id'=>'zca_id','title'=>'zca_title');
		}elseif($adzone->zdz_associate==2){
			$key = array('id'=>'znc_id','title'=>'znc_title');
			$category = NewsCat::model()->getNewsCat();
			//print_r($category);exit;
		}
		//若存在父菜单
		if($menu1 && $menu1->zna_parentid!=0){
			$menu2 = Nav::model()->findByPk($menu1->zna_parentid);
			//保证$menu1是一级菜单,以此类推
			list($menu1,$menu2) = array($menu2,$menu1);
			if($menu1 && $menu1->zna_parentid!=0){
				$menu3 = Nav::model()->findByPk($menu1->zna_parentid);
				list($menu1,$menu2,$menu3) = array($menu3,$menu1,$menu2);
			}
		}
		$this->render('update',array(
				'model'=>$model,
				'menu1'=>$menu1,
				'menu2'=>$menu2,
				'menu3'=>$menu3,
				'adzone'=>$adzone,
				'category'=>$category,
				'catkey'=>$key,
		));
	}

	/**
	 * 单条批量删除
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete()
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;

		$criteria->addCondition("zds_id in ($id)");		
		//执行操作
		if(Adse::model()->deleteAll($criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Adse');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Adse('search');
		$model->unsetAttributes();
		if(isset($_GET['Adse']))
			$model->attributes=$_GET['Adse'];
		$model->zdz_id = $model->zdz_id<1 ? '' : $model->zdz_id;
		//菜单数据
		$zna_id[1] = isset($_GET['zna_id']) ? $_GET['zna_id'] : 0;
		$zna_id[2] = isset($_GET['zna_id2']) ? $_GET['zna_id2'] : 0;
		$zna_id[3] = isset($_GET['zna_id3']) ? $_GET['zna_id3'] : 0;
		//选中的最低级菜单
		$zna_id[0] = $zna_id[1];
		$zna_id[0] = !$zna_id[2] ? $zna_id[0] : $zna_id[2];
		$zna_id[0] = !$zna_id[3] ? $zna_id[0] : $zna_id[3];
		$model->znaid = $zna_id[0] = $zna_id[0]<1 ? '' : $zna_id[0];
		//关键字
		$model->zds_title = $model->zds_title=='关键字' ? '' : trim($model->zds_title);
		//状态
		$model->status = isset($_GET['status']) ? $_GET['status'] : 0;
		//广告位id，从广告位查看广告
		$model->zdz_id  = isset($_GET['zdz_id']) ? (int)$_GET['zdz_id'] : $model->zdz_id;
		//关联分类，从分类查看广告
		$model->catid   = isset($_GET['catid']) ? $_GET['catid'] : null;
		$model->cattype = isset($_GET['cattype']) ? $_GET['cattype'] : null;		

		$this->render('admin',array(
			'zna_id'=>$zna_id,
			'model'=>$model,
		));
	}

	/**
	 * 单条，批量下线
	 */
	public function actionBatchChange()
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zds_id in ($id)");
		//修改内容
		$set = array('zds_end_time'=>time());
		//执行操作
		if(Adse::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}

	/**
	 * 显示产品分类广告
	 */
	public function actionProCatAds()
	{
		$this->render('procatadmin');
	}

	/**
	 * 显示产品分类广告
	 */
	public function actionNewsCatAds()
	{
		$model=new NewsCat('search');
		$model->unsetAttributes();  // clear any default values
		$model->znc_patentid = 0;
		
		$this->render('newscatadmin',array(
				'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Adse::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='adse-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
